# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::layer::Planet;

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	$self->{View} = shift;
	$self->{Empire} = shift;
	bless ($self, $class);
	return $self;
}

sub showPlanet
{
	my $self = shift;
	my $planet = shift;
	my $empire = $self->{Empire};
	my ($x, $y) = map {$planet->getProp($_)} qw(x y);
	my ($tx, $ty) = ($self->{Canvas}->{min_x} > $x ? $x + 2000 : $x, $self->{Canvas}->{min_y} > $y ? $y + 2000 : $y);
	return if ($tx > $self->{Canvas}->{max_x} || $ty > $self->{Canvas}->{max_y});
	my ($s, $fg, $bg, $text) = $self->{View}->Transform($planet, $empire);
	my $canvas = $self->{Canvas};
	$canvas->Circle($canvas->center($tx, $ty), $s, $fg, $bg);
	$canvas->Text($canvas->center($tx, $ty), -1, -1, $text, $bg) if defined $text;
}

sub setEmpire
{
	my $self = shift;
	$self->{Empire} = shift;
}

sub display
{
	my $self = shift;
	$self->{Canvas} = shift;
	my $empire = $self->{Empire};
#	$self->showPlanet($_, $empire) foreach $empire->planets->getAllKnown;
	$self->showPlanet($_, $empire) foreach $empire->planets->getRectangle(
		$self->{Canvas}->{min_x}, $self->{Canvas}->{min_y}, 
		$self->{Canvas}->{max_x}, $self->{Canvas}->{max_y});
}

1;

